/******************************************************************************
@file:  DoctorsVisit.java
@author Ramesh Anandhan

DESCRIPTION
-----------------------------------------------------------------------------
Copyright (c) 2011, GEEKWAVE SOLUTIONS INC.

All rights reserved.
 ******************************************************************************/

package com.smartc.health;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.smartc.connection.HttpConnection;
import com.smartc.ui.AccountLogin;
import com.smartc.ui.R;

import android.app.Activity;
import android.content.Context; //import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DoctorsVisit extends Activity implements OnItemClickListener {
	protected static final String TAG = "DoctersVisit";
	private ArrayList<Object> itemList;
	private ItemBean bean;

	ListView mItemListView;
	ListViewAdapter adapter;

	private HttpConnection mCityConnection;
	private ResponseHandler mResponse = new ResponseHandler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.doctor_visit);
		mCityConnection = new HttpConnection();
		mCityConnection.AddHandler(mResponse);
	}

	@Override
	protected void onStart() {
		super.onStart();
		String domain = getResources().getString(R.string.domain);
		String query = getResources()
				.getString(R.string.url_health_care_doctor);
		String url = domain + query;
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onStart url:" + url);

		mCityConnection.setUrl(url);
		mCityConnection.clearParam();
		mCityConnection.Execute();
	}

	public static class ViewHolder {
		ImageView ItemImage;
		TextView ItemName;
		TextView ItemDesc;
		TextView ItemsTime;
		TextView ItemLocation;
		TextView Itemcontact;
	}

	private class ListViewAdapter extends BaseAdapter {

		private LayoutInflater inflater;
		// private Context mContext;
		ArrayList<Object> mItemList;

		ListViewAdapter(Context context, ArrayList<Object> itemList) {
			super();
			// mContext = context;
			mItemList = itemList;
			this.inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return mItemList.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = inflater
						.inflate(R.layout.doctor_visit_list, null);

				holder.ItemImage = (ImageView) convertView
						.findViewById(R.id.doctor_image);
				holder.ItemName = (TextView) convertView
						.findViewById(R.id.doctot_text_name);
				holder.ItemDesc = (TextView) convertView
						.findViewById(R.id.doctor_text_desc);
				holder.ItemsTime = (TextView) convertView
						.findViewById(R.id.doctor_visit_time);
				holder.ItemLocation = (TextView) convertView
						.findViewById(R.id.doctor_location);
				holder.Itemcontact = (TextView) convertView
						.findViewById(R.id.doctor_text_contact);

				convertView.setTag(holder);
			} else
				holder = (ViewHolder) convertView.getTag();

			ItemBean bean = (ItemBean) itemList.get(position);

			holder.ItemImage.setImageResource(bean.getImage());
			holder.ItemName.setText(bean.getItemName());
			holder.ItemsTime.setText(bean.getItemTime());
			holder.ItemDesc.setText(bean.getItemDesc());
			holder.ItemLocation.setText(bean.getItemLocation());
			holder.Itemcontact.setText(bean.getItemContact());
			return convertView;
		}
	}

	public class ItemBean {
		String mItemName;
		String mItemTime;
		String mItemDesc;
		String mItemLocation;
		String mItemcontact;
		int image;

		public String getItemName() {
			return mItemName;
		}

		public void setItemName(String name) {
			this.mItemName = name;
		}

		public String getItemDesc() {
			return mItemDesc;
		}

		public void setItemDesc(String desc) {
			this.mItemDesc = desc;
		}

		public String getItemTime() {
			return mItemTime;
		}

		public void setItemTime(String itemtime) {
			this.mItemTime = itemtime;
		}

		public String getItemContact() {
			return mItemcontact;
		}

		public void setItemContact(String contact) {
			this.mItemcontact = contact;
		}

		public int getImage() {
			return image;
		}

		public void setImage(int image) {
			this.image = image;
		}

		public String getItemLocation() {
			return mItemLocation;
		}

		public void setItemLocation(String location) {
			this.mItemLocation = location;
		}

	}

	public void AddObjectToList(int image, String name, String desc,
			String time, String contact, String location) {

		bean = new ItemBean();
		bean.setItemName(name);
		bean.setItemDesc(desc);
		bean.setItemContact(contact);
		bean.setItemTime(time);
		bean.setImage(image);
		bean.setItemLocation(location);
		itemList.add(bean);
	}

	public class ResponseHandler extends DefaultHandler {

		private boolean idSpecialistDoctor = false;
		private boolean nmSpecialistDoctor = false;
		private boolean nmMedicalSpecialist = false;
		//private boolean tsVisit = false;
		private boolean nmContactNumber = false;
		private boolean nmLocation = false;
		private boolean dtFrom = false;
		private boolean dtTo = false;
		private boolean tsFrom = false;
		private boolean tsTo = false;

		public ArrayList<String> mDoctorName_List = new ArrayList<String>();
		public ArrayList<String> mDoctorNameId_List = new ArrayList<String>();
		public ArrayList<String> mDoctorDesc_List = new ArrayList<String>();
		
		public ArrayList<String> mContact_List = new ArrayList<String>();
		public ArrayList<String> mLocation_List = new ArrayList<String>();
		public ArrayList<String> mdtFrom_List = new ArrayList<String>();
		public ArrayList<String> mdtTo_List = new ArrayList<String>();
		public ArrayList<String> mtsFrom_List = new ArrayList<String>();
		public ArrayList<String> mtsTo_List = new ArrayList<String>();
		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (this.idSpecialistDoctor) {
				mDoctorNameId_List.add(new String(ch, start, length));
			} else if (this.nmSpecialistDoctor) {
				mDoctorName_List.add(new String(ch, start, length));
			} else if (this.nmMedicalSpecialist) {
				mDoctorDesc_List.add(new String(ch, start, length));
			} else if (this.nmContactNumber) {
				mContact_List.add(new String(ch, start, length));
			} else if (this.nmLocation) {
				mLocation_List.add(new String(ch, start, length));
			} else if (this.dtFrom) {
				mdtFrom_List.add(new String(ch, start, length));
			} else if (this.dtTo) {
				mdtTo_List.add(new String(ch, start, length));
			} else if (this.tsFrom) {
				mtsFrom_List.add(new String(ch, start, length));
			} else if (this.tsTo) {
				mtsTo_List.add(new String(ch, start, length));
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			if (localName.equals("DoctorList")) {
				Message myMessage = new Message();
				myMessage.obj = "DOCTORS";
				handler.sendMessage(myMessage);
			} else if (localName.equals("idSpecialistDoctor")) {
				this.idSpecialistDoctor = false;
			} else if (localName.equals("nmSpecialistDoctor")) {
				this.nmSpecialistDoctor = false;
			} else if (localName.equals("nmMedicalSpecialist")) {
				this.nmMedicalSpecialist = false;
			} else if (localName.equals("tsVisit")) {
				//this.tsVisit = false;
			} else if (localName.equals("nmContactNumber")) {
				this.nmContactNumber = false;
			} else if (localName.equals("nmLocation")) {
				this.nmLocation = false;
			} else if (localName.equals("dtFrom")) {
				this.dtFrom = false;
			} else if (localName.equals("dtTo")) {
				this.dtTo = false;
			} else if (localName.equals("tsFrom")) {
				this.tsFrom = false;
			} else if (localName.equals("tsTo")) {
				this.tsTo = false;
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			if (localName.equals("DoctorList")) {
				// this.TradeProducts = true;
				mDoctorName_List.clear();
				mDoctorNameId_List.clear();
				mDoctorDesc_List.clear();
				mdtFrom_List.clear();
				mdtTo_List.clear();
				mtsFrom_List.clear();
				mtsTo_List.clear();
				mContact_List.clear();
				mLocation_List.clear();
			} else if (localName.equals("idSpecialistDoctor")) {
				this.idSpecialistDoctor = true;
			} else if (localName.equals("nmSpecialistDoctor")) {
				this.nmSpecialistDoctor = true;
			} else if (localName.equals("nmMedicalSpecialist")) {
				this.nmMedicalSpecialist = true;
			} else if (localName.equals("tsVisit")) {
				//this.tsVisit = true;
			} else if (localName.equals("nmContactNumber")) {
				this.nmContactNumber = true;
			} else if (localName.equals("nmLocation")) {
				this.nmLocation = true;
			} else if (localName.equals("dtFrom")) {
				this.dtFrom = true;
			} else if (localName.equals("dtTo")) {
				this.dtTo = true;
			} else if (localName.equals("tsFrom")) {
				this.tsFrom = true;
			} else if (localName.equals("tsTo")) {
				this.tsTo = true;
			}
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage msg:" + (String) msg.obj);
			String loginmsg = (String) msg.obj;
			ArrayList<String> mTime_List = new ArrayList<String>();
			
			/*if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage mResponse.mdtFrom_List.size:" + mResponse.mdtFrom_List.size());
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage mResponse.mtsFrom_List.size:" + mResponse.mtsFrom_List.size());
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage mResponse.mdtTo_List.size:" + mResponse.mdtTo_List.size());
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage mResponse.mtsTo_List.size:" + mResponse.mtsTo_List.size());
			
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage mResponse.mDoctorDesc_List.size:" + mResponse.mDoctorDesc_List.size());
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage mResponse.mDoctorName_List.size:" + mResponse.mDoctorName_List.size());
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage mResponse.mLocation_List.size:" + mResponse.mLocation_List.size());
			if (AccountLogin.DEBUG)
				Log.i(TAG, "handleMessage mResponse.mContact_List.size:" + mResponse.mContact_List.size());*/
			
			/*for (int i = 0; i < mResponse.mDoctorName_List.size(); i++) {
				if (AccountLogin.DEBUG)
					Log.i(TAG, "handleMessage mResponse.mDoctorName_List:" +i+"->"+mResponse.mDoctorName_List.get(i));
			}*/
			
			for(int i=0;i<mResponse.mdtFrom_List.size();i++){
				mTime_List.add(mResponse.mdtFrom_List.get(i)
						       +" "+mResponse.mtsFrom_List.get(i)
						       +" to "+mResponse.mdtTo_List.get(i)
						       +" "+mResponse.mtsTo_List.get(i));
			}
			
			
			if (loginmsg != null && loginmsg.equals("DOCTORS")) {
				itemList = new ArrayList<Object>();
				for (int i = 0; i < mResponse.mDoctorName_List.size(); i++) {
					AddObjectToList(R.drawable.doctor,
							mResponse.mDoctorName_List.get(i),
							mResponse.mDoctorDesc_List.get(i),
							mTime_List.get(i),
							mResponse.mContact_List.get(i),
							mResponse.mLocation_List.get(i));
				}

				mItemListView = (ListView) findViewById(R.id.doctor_list);
				adapter = new ListViewAdapter(DoctorsVisit.this
						.getApplicationContext(), itemList);
				mItemListView.setAdapter(adapter);
				mItemListView.setOnItemClickListener(DoctorsVisit.this);
			}

		}
	};

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int position,
			long arg3) {
		if (AccountLogin.DEBUG)
			Log.i(TAG, "onItemClick position" + position);
		/*
		 * Intent intent = new
		 * Intent(DoctorsVisit.this,DoctorsRegistration.class);
		 * intent.putExtra("docname", mResponse.mDoctorName_List.get(position));
		 * intent.putExtra("specalist",
		 * mResponse.mDoctorDesc_List.get(position)); intent.putExtra("address",
		 * mResponse.mContact_List.get(position)); intent.putExtra("time",
		 * mResponse.mTime_List.get(position)); startActivity(intent);
		 */
	}

}
